﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace client
{
    public delegate void LedGroupListChanged(string[] groups);
    public partial class LedChannelControl : UserControl
    {
        private int channel = 1;
        private string group = "";

        public static event LedGroupListChanged GroupsChanged;

        public Connection Connection { get; set; }

        private static List<string> ledGroups = new List<string>();
        private static List<LedChannelControl> ledChannels = new List<LedChannelControl>();
        public static void AddGroup(string ledGroup)
        {
            if (string.IsNullOrEmpty(ledGroup))
                return;

            if (!string.IsNullOrEmpty(ledGroups.Find((s) => { return ledGroup == s; })))
                return;

            ledGroups.Add(ledGroup);
            RefreshGroups();

            if (GroupsChanged != null)
                GroupsChanged(ledGroups.ToArray());
        }

        public static bool RemoveGroup(string ledGroup)
        {
            if (string.IsNullOrEmpty(ledGroups.Find((s) => { return ledGroup == s; })))
                return true;

            foreach (LedChannelControl ch in ledChannels)
            {
                if (ch.comboBox1.Text == ledGroup)
                    return false;
            }

            ledGroups.Remove(ledGroup);
            RefreshGroups();

            if (GroupsChanged != null)
                GroupsChanged(ledGroups.ToArray());
            return true;
        }

        public static void UpdateGroup(string group, double value)
        {
            foreach (LedChannelControl ch in ledChannels)
            {
                if (group != ch.Group)
                    continue;

                ch.Level = value;
            }
        }

        private static void RefreshGroups()
        {
            foreach (LedChannelControl ch in ledChannels)
            {
                int index = -1;
                string selected = ch.comboBox1.SelectedText;
                ch.comboBox1.BeginUpdate();
                ch.comboBox1.Items.Clear();
                foreach (string g in ledGroups)
                {
                    if (g == selected)
                        index = ledGroups.Count;

                    ch.comboBox1.Items.Add(g);
                }
                ch.comboBox1.SelectedIndex = index;
                ch.comboBox1.EndUpdate();
            }
        }

        [Browsable(true)]
        public int Channel
        {
            get { return channel; }
            set
            {
                channel = value;
                label1.Text = string.Format("Channel {0}:", channel);
            }
        }

        [Browsable(true)]
        public string Group
        {
            get { return group; }
            set
            {
                group = value;
                comboBox1.Text = group;
            }
        }

        [Browsable(true)]
        public double Level 
        {
            get { return textBox1.Value; } 
            set 
            { 
                textBox1.Value = value;
                LedLevelUpdate(textBox1, value);
            } 
        }

        public LedChannelControl()
        {
            ledChannels.Add(this);

            InitializeComponent();

            textBox1.FineStep = 1;
            textBox1.CourseStep = 10;
            textBox1.TextUpdate += new TextUpdate(LedLevelUpdate);

            Height = comboBox1.Height;
        }

        public void Initialize()
        {
            LedLevelUpdate(textBox1, 0);
        }

        private void LedLevelUpdate(object sender, double value)
        {
            if (!Connection.IsConnected)
                return;

            Connection.SendCommand(string.Format("setled {0} {1}", channel - 1, (int)Math.Ceiling((0xffe / 100.0) * value)));
        }

        private void LedChannelControl_Resize(object sender, EventArgs e)
        {
            Height = comboBox1.Height;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox1.Enabled = !checkBox1.Checked;
            comboBox1.Enabled = checkBox1.Checked;

            string remove = comboBox1.Text;
            comboBox1.Text = "";
            LedChannelControl.RemoveGroup(remove);
        }

        private void comboBox1_Leave(object sender, EventArgs e)
        {
            AddGroup(comboBox1.Text);

            if (group != comboBox1.Text)
                LedChannelControl.RemoveGroup(group);
            group = comboBox1.Text;
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
